package com.alibaba.dubbo.demo.provider.dao.impl;

import java.util.List;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.demo.SysRoleInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysRole;
import com.alibaba.dubbo.demo.bean.SysUserinfo;

public class SysRoleDaoImpl extends BaseDAO implements SysRoleInterface {

	
	@Override
	public PageBean queryAllSysRole(PageBean pageBean, String rolecode, String rolename) {
		// TODO Auto-generated method stub
		System.out.println("enter queryAllSysRole");
		String msql = "from SysRole";
		String where = " where 1=1 ";
		if(StringUtils.isNotEmpty(rolecode))
			where += " and rolecode like '%"+rolecode+"%'";
		if(StringUtils.isNotEmpty(rolename))
			where += " and rolename like '%"+rolename+"%'";
		
		msql = msql + where;
		
		String countSql = "select count(*) " + msql;
		System.out.println("enter queryAllSysRole msql="+msql);
		System.out.println("enter queryAllSysRole countSql="+countSql);
		return getPageBean(msql, countSql, pageBean);
	}

	@Override
	public List<SysRole> queryAllSysRoleList(String rolecode, String rolename) {
		// TODO Auto-generated method stub
		String msql = "from SysRole";
		String where = " where 1=1 ";
		if(StringUtils.isNotEmpty(rolecode))
			where += " and rolecode like '%"+rolecode+"%'";
		if(StringUtils.isNotEmpty(rolename))
			where += " and rolename like '%"+rolename+"%'";

		if(where!="")
			msql = msql + where;
		return queryBean(msql);	
	}

	@Override
	public SysRole queryOneSysRole(String rolecode) {
		// TODO Auto-generated method stub
		String hsql = "from SysRole where rolecode='"+rolecode+"'";
		if(queryBean(hsql).size()>0)
			return (SysRole) queryBean(hsql).get(0);
		return null;
	}
	
	@Override
	public SysRole queryOneSysRole(int roleid) {
		// TODO Auto-generated method stub
		String hsql = "from SysRole where roleid="+roleid;
		if(queryBean(hsql).size()>0)
			return (SysRole) queryBean(hsql).get(0);
		return null;
	}

	@Override
	public boolean saveSysRole(SysRole sysRole) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().save(sysRole);
			return true;
		} catch (Exception e) {
			logger.error("新增通知异常！", e);
			return false;
		}
	}

	@Override
	public boolean updateSysRole(SysRole sysRole) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().update(sysRole);
			return true;
		} catch (Exception e) {
			logger.error("更新通知异常！", e);
			return false;
		}
	}

	@Override
	public boolean deleteSysRole(int roleid) {
		// TODO Auto-generated method stub
		SysRole sysRole = queryOneSysRole(roleid);
		if(sysRole==null)
			return false;
		try {
			 getHibernateTemplate().delete(sysRole);
			 return true;
		} catch (Exception e) {
			logger.error("删除通知异常！", e);
			return false;
		}
	}

	@Override
	public boolean updateSysRoleFlag(int roleid, int valid) {
		// TODO Auto-generated method stub
		SysRole sysRole = queryOneSysRole(roleid);
		if(sysRole==null)
			return false;
		sysRole.setIsvalid(valid);
		return updateSysRole(sysRole);
	}
}
